#pragma once
#include <iostream>
#include <cassert>
namespace zsw
{
	class vector
	{
	public:
		vector()
			:_arr(new int[8])
			, _size(0)
			, _capacity(8)
		{}

		vector(int n)
			:_arr(new int[n])
			, _size(0)
			, _capacity(n)
		{}

		vector(int n, int val)
			:_arr(new int[n])
			, _size(0)
			, _capacity(n)
		{
			for (int i = 0; i < n; i++)
				_arr[i] = val;
		}

		void push_back(int val)
		{
			if (_size == _capacity)
			{
				reserve(_size * 2);
			}

			_arr[_size] = val;
			_size++;
		}

		void pop_back()
		{
			assert(_size > 0);
			_size--;
		}

		void reserve(int n)
		{
			if (n <= _size)
				return;
			int* arr = new int[n];

			for (int i = 0; i < _size; i++)
			{
				arr[i] = _arr[i];
			}

			std::swap(arr, _arr);
			delete[] arr;
			_capacity = n;
		}

		void resize(int n)
		{
			if (n <= _size)
			{
				_size = n;
				return;
			}
			else
			{
				if (n > _capacity)
				{
					reserve(n);
				}

				_size = n;
			}
		}

		~vector()
		{
			if (_arr)
				delete[] _arr;

			_size = _capacity = 0;
		}
	private:
		int* _arr;
		size_t _size;
		size_t _capacity;
	};
}